package request

import (
	"encoding/json"
	"fmt"
	"go.uber.org/zap"
	"maya-service/collector/trace_log"
	"maya-service/constant"
	"maya-service/model/po/mall"
	"maya-service/utils"
	"time"
)

// GetGoodsInfo 查询sku
func GetGoodsInfo(token string, env string, goodsId int64) (productInfo mall.GoodsData, err error) {
	configUrl := constant.TestShopConfigUrl + "/v3/open/seller/goods/query"
	if env == "prod" {
		configUrl = constant.ProdShopConfigUrl + "/v3/open/seller/goods/query"
	}

	if goodsId > 0 {
		configUrl = fmt.Sprintf("%v?goodsId=%v", configUrl, goodsId)
	}

	rsp, err := DoRequest("GET", configUrl, "", map[string]string{
		"Authorization": token,
	})

	if err != nil {
		fmt.Println(err)
		return
	}

	// 最外层 JSON 结构
	type Response struct {
		Data      mall.GoodsData `json:"data"`
		LogMsg    *string        `json:"logMsg"`
		Msg       string         `json:"msg"`
		State     int            `json:"state"`
		Timestamp int64          `json:"timestamp"`
	}

	ret := &Response{}
	err = json.Unmarshal([]byte(rsp), ret)
	if err != nil {
		fmt.Println(err)
		return
	}

	if ret.State != 200 {
		err = fmt.Errorf(ret.Msg)
		return
	}

	return ret.Data, nil
}

// CreateOrUpdateGoods 创建或者更新商品
func CreateOrUpdateGoods(token string, env string, goodsInfo mall.CreateGoodsRequest) (out mall.CreateGoodsResponse, err error) {
	since := time.Now()
	defer func() {
		trace_log.CommonLogger.Info("####### 调用商城 CreateOrUpdateGoods data:", utils.ToJsonString(goodsInfo), " err:", err, " 耗时：", time.Since(since))
	}()
	configUrl := constant.TestShopConfigUrl + "/v3/open/seller/goods/editGoods"
	if env == "prod" {
		configUrl = constant.ProdShopConfigUrl + "/v3/open/seller/goods/editGoods"
	}

	// 创建商品
	rsp, err := DoRequest("POST", configUrl, utils.ToJsonString(goodsInfo), map[string]string{
		"Authorization": token,
	})

	trace_log.CommonLogger.Info("####### 调用商城创建合集 CreateOrUpdateGoods", zap.Any("token", token), zap.Any("data", utils.ToJsonString(goodsInfo)), zap.Any("rsp", rsp), zap.Any("err", err))

	if err != nil {
		fmt.Println(err)
		return
	}

	// 最外层 JSON 结构
	type Response struct {
		Data      mall.CreateGoodsResponse `json:"data"`
		LogMsg    *string                  `json:"logMsg"`
		Msg       string                   `json:"msg"`
		State     int                      `json:"state"`
		Timestamp int64                    `json:"timestamp"`
	}

	ret := &Response{}
	err = json.Unmarshal([]byte(rsp), ret)
	if err != nil {
		fmt.Println(err)
		return
	}

	if ret.State != 200 {
		err = fmt.Errorf(ret.Msg)
		return
	}

	return ret.Data, nil
}

// CreateOrUpdateProduct 创建或者更新商品
func CreateOrUpdateProduct(token string, env string, productInfo mall.CreateProductRequest) (out mall.CreateProductResponse, err error) {
	since := time.Now()
	defer func() {
		trace_log.CommonLogger.Info("####### 调用商城 CreateOrUpdateProduct data:", utils.ToJsonString(productInfo), " err:", err, " 耗时：", time.Since(since))
	}()
	configUrl := constant.TestShopConfigUrl + "/v3/open/seller/goods/editProductList"
	if env == "prod" {
		configUrl = constant.ProdShopConfigUrl + "/v3/open/seller/goods/editProductList"
	}

	// 创建商品
	rsp, err := DoRequest("POST", configUrl, utils.ToJsonString(productInfo), map[string]string{
		"Authorization": token,
	})

	trace_log.CommonLogger.Info("####### 调用商城 CreateOrUpdateProduct", zap.Any("token", token), zap.Any("data", utils.ToJsonString(productInfo)), zap.Any("rsp", rsp), zap.Any("err", err))

	if err != nil {
		fmt.Println(err)
		return
	}

	// 最外层 JSON 结构
	type Response struct {
		Data      mall.CreateProductResponse `json:"data"`
		LogMsg    *string                    `json:"logMsg"`
		Msg       string                     `json:"msg"`
		State     int                        `json:"state"`
		Timestamp int64                      `json:"timestamp"`
	}

	ret := &Response{}
	err = json.Unmarshal([]byte(rsp), ret)
	if err != nil {
		fmt.Println(err)
		return
	}

	if ret.State != 200 {
		err = fmt.Errorf(ret.Msg)
		return
	}

	return ret.Data, nil
}
